/**
 * CalEvent class following specifications for iCal calendaring
 * objects as specified by RFC5545. Creates the events to add
 * to Calendar object.
 * 
 * @author nisoda, shultzam
 */
package application;

/**
 * @author nisoda, shultzam
 */
public class CalEvent {
	
	private String summary;
	private String dtStart;
	private int dStart;
	private int tStart;
	private String dtEnd;
	private int dEnd;
	private int tEnd;
	private int priority;
	private double geoLat;
	private double geoLong;
	private String classification;

	/**
	 * Constructor for CalEvent object.
	 * @param summary
	 */
	public CalEvent(String summary) {
		this.summary = summary;
		this.dtStart = new String();
		this.dStart = 0;
		this.tStart = 0;
		this.dtEnd = new String();
		this.dEnd = 0;
		this.tEnd = 0;
		this.classification = new String();
	}
	
	/**
	 * Sets the Start Date/Time for the event.
	 * @param date
	 * @param time
	 */
	public void setDTStart(String date, String time) {
		this.dStart = Integer.parseInt(date);
		this.tStart = Integer.parseInt(time);
	}
	
	/**
	 * Sets the End Date/Time for the event.
	 * @param date
	 * @param time
	 */
	public void setDTEnd(String date, String time) {
		this.dEnd = Integer.parseInt(date);
		this.tEnd = Integer.parseInt(time);
	}
	
	/**
	 * A really long, roundabout, brute-force way to adjust dates/times
	 * according to a given Time Zone offset from GMT
	 * 
	 * @param tzOffset
	 */
	public void adjustTZOffset(int tzOffset) {
		//Adjust Start Date/Time
		int tempTime = this.tStart - tzOffset;
		
		if(tempTime < 0) {
			this.tStart = tempTime + 2400;
			
			if(Integer.toString(this.dStart).substring(6,8).compareTo("01") == 0 ) {
				switch(Integer.toString(this.dStart).substring(4,6)) {
					case "01" : this.dStart -= 10000;
				    			this.dStart += 1130;
								break;
		    		case "02" :
		    		case "04" :
		    		case "06" :
		    		case "08" :
		    		case "09" : 
		    		case "11" : this.dStart -= 100;
				    			this.dStart += 30;
								break;
		    		case "05" :
		    		case "07" :
		    		case "10" :
		    		case "12" : this.dStart -= 100;
					    		this.dStart += 29;
					    		break;
		    		case "03" : if((Integer.parseInt(Integer.toString(this.dStart).substring(0,4)) % 4) == 0 ) {
					        		this.dStart -= 100;
						    		this.dStart += 28;
					        	}
		    					else {
						    		this.dStart -= 100;
						    		this.dStart += 27;
						    	}
								break;
					default : this.dStart -= 1;
							  break;
				}
			}
    		else {
    			this.dStart -= 1;
    		}
		}
		else if (tempTime >= 2400) {
			this.tStart = tempTime - 2400;
			
			switch(Integer.toString(this.dStart).substring(4,6)) {
				case "01" :
	    		case "03" :
	    		case "05" :
	    		case "07" :
	    		case "08" :
	    		case "10" : if(Integer.toString(this.dStart).substring(6,8).compareTo("31") == 0 ) {
				    			this.dStart += 100;
				    			this.dStart -= 30;
				    		}
				    		else {
				    			this.dStart += 1;
				    		}
				    		break;
	    		case "12" : if(Integer.toString(this.dStart).substring(6,8).compareTo("31") == 0 ) {
	    						this.dStart += 10000;
				    			this.dStart -= 1130;
				    		}
							break;
	    		case "04" :
	    		case "06" :
	    		case "09" :
	    		case "11" : if(Integer.toString(this.dStart).substring(6,8).compareTo("30") == 0 ) {
				    			this.dStart += 100;
				    			this.dStart -= 29;
				    		}
				    		else {
				    			this.dStart += 1;
				    		}
				    		break;
	    		case "02" : if((Integer.parseInt(Integer.toString(this.dStart).substring(0,4)) % 4) == 0 ) {
				        		if(Integer.toString(this.dStart).substring(6,8).compareTo("29") == 0 ) {
				        			this.dStart += 100;
					    			this.dStart -= 28;
				        		}
				        	}
				    		else if(Integer.toString(this.dStart).substring(6,8).compareTo("29") == 0 ) {
				    			this.dStart += 100;
				    			this.dStart -= 27;
				    		}
				    		else {
				    			this.dStart += 1;
				    		}
							break;
				default : 	this.dStart += 1;
							break;
			}
		}
		else {
			this.tStart = tempTime;
		}		
		
		//Adjust End Date/Time
		tempTime = this.tEnd - tzOffset;
		if(tempTime < 0) {
			this.tEnd = tempTime + 2400;
			
			if(Integer.toString(this.dEnd).substring(6,8).compareTo("01") == 0 ) {
				switch(Integer.toString(this.dEnd).substring(4,6)) {
					case "01" : this.dEnd -= 10000;
				    			this.dEnd += 1130;
								break;
		    		case "02" :
		    		case "04" :
		    		case "06" :
		    		case "08" :
		    		case "09" : 
		    		case "11" : this.dEnd -= 100;
				    			this.dEnd += 30;
								break;
		    		case "05" :
		    		case "07" :
		    		case "10" :
		    		case "12" : this.dEnd -= 100;
					    		this.dEnd += 29;
					    		break;
		    		case "03" : if((Integer.parseInt(Integer.toString(this.dEnd).substring(0,4)) % 4) == 0 ) {
					        		this.dEnd -= 100;
						    		this.dEnd += 28;
					        	}
		    					else {
						    		this.dEnd -= 100;
						    		this.dEnd += 27;
						    	}
								break;
					default : this.dEnd -= 1;
							  break;
				}
			}
    		else {
    			this.dEnd -= 1;
    		}
		}
		else if (tempTime >= 2400) {
			this.tEnd = tempTime - 2400;
			
			switch(Integer.toString(this.dEnd).substring(4,6)) {
				case "01" :
	    		case "03" :
	    		case "05" :
	    		case "07" :
	    		case "08" :
	    		case "10" : if(Integer.toString(this.dEnd).substring(6,8).compareTo("31") == 0 ) {
				    			this.dEnd += 100;
				    			this.dEnd -= 30;
				    		}
				    		else {
				    			this.dEnd += 1;
				    		}
				    		break;
	    		case "12" : if(Integer.toString(this.dEnd).substring(6,8).compareTo("31") == 0 ) {
	    						this.dEnd += 10000;
				    			this.dEnd -= 1130;
				    		}
				    		else {
				    			this.dEnd += 1;
				    		}
							break;
	    		case "04" :
	    		case "06" :
	    		case "09" :
	    		case "11" : if(Integer.toString(this.dEnd).substring(6,8).compareTo("30") == 0 ) {
				    			this.dEnd += 100;
				    			this.dEnd -= 29;
				    		}
				    		else {
				    			this.dEnd += 1;
				    		}
				    		break;
	    		case "02" : if((Integer.parseInt(Integer.toString(this.dEnd).substring(0,4)) % 4) == 0 ) {
				        		if(Integer.toString(this.dEnd).substring(6,8).compareTo("29") == 0 ) {
				        			this.dEnd += 100;
					    			this.dEnd -= 28;
				        		}
				        	}
				    		else if(Integer.toString(this.dEnd).substring(6,8).compareTo("29") == 0 ) {
				    			this.dEnd += 100;
				    			this.dEnd -= 27;
				    		}
				    		else {
				    			this.dEnd += 1;
				    		}
							break;
				default : this.dEnd += 1;
						  break;
			}
		}
		else {
			this.tEnd = tempTime;
		}
	}
	
	/**
	 * Sets the Classification for the event.
	 * @param classif
	 */
	public void setClassif(String classif) {
		this.classification = classif;
	}
	
	/**
	 * Sets the Priority for the event.
	 * @param priority
	 */
	public void setPriority(int priority) {
		this.priority = priority;
	}
	
	/**
	 * Sets the Geographic location for the event.
	 * @param lati
	 * @param longi
	 */
	public void setGeo(double lati, double longi) {
		this.geoLat = lati;
		this.geoLong = longi;
	}

	/**
	 * Returns a String representation of this CalEvent object.
	 */
	@Override
	public String toString() {
		String eventString = new String();
		
		dtStart = String.format("%dT%04d00Z", this.dStart, this.tStart);
		dtEnd = String.format("%dT%04d00Z", this.dEnd, this.tEnd);
		
		eventString = String.format("BEGIN:VEVENT%n"
				+ "DTSTART:%s%n" + "DTEND:%s%n" + "CLASS:%s%n"
				+ "PRIORITY:%d%n", this.dtStart,
				this.dtEnd,	this.classification, this.priority);
		
		if((this.geoLat != 0) || (this.geoLong != 0)) {
			eventString.concat(String.format("GEO:%f;%f%n", this.geoLat, this.geoLong));
		}
		eventString = eventString.concat(String.format("SUMMARY:%s%n", this.summary));
		eventString = eventString.concat(String.format("END:VEVENT%n"));
		
		return eventString;
	}
}